package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.AdapterItem1Heigth40TextSize15;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.BranchBankEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by syd on 2016/7/28.
 */
public class SettleBanknameActivity extends BaseActivity {


    List<BranchBankEntity> branchBankEntities;
    List<String> listDatas = new ArrayList<String>();
    AdapterItem1Heigth40TextSize15 adapter;
    String[] str;
    @BindView(R.id.lv_settlebankname_bankname)
    ListView lv_settlebankname_bankname;
    @BindView(R.id.et_settlebankname_bankname)
    EditText et_settlebankname_bankname;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settlebankname);
        initTitle("选择支行");
        ButterKnife.bind(this);
        initData();
        lv_settlebankname_bankname.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String bankName = ((TextView) view.findViewById(R.id.tv_adapter_item1_height40_textsize15)).getText().toString();
                String cardCode = findCardCodeByBankName(bankName);
                Intent intent = new Intent(mContext, AddSettleBankcardActivity.class);
                intent.putExtra("httpActionType",getIntent().getStringExtra("httpActionType"));
                intent.putExtra("bankName",bankName);
                intent.putExtra("cardCode",cardCode);
                intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                intent.putExtra("bankType",getIntent().getStringExtra("bankType"));
                intent.putExtra("bankphone",getIntent().getStringExtra("bankphone"));
                intent.putExtra("bankSimpleName",getIntent().getStringExtra("bankSimpleName"));
                startActivity(intent);
                finish();
            }
        });
    }

    private String findCardCodeByBankName(String bankName) {

        for (BranchBankEntity branchBankEntity:branchBankEntities) {
            if (bankName.equals(branchBankEntity.getBankName())) {
                return branchBankEntity.getBankCode();
            }
        }
        return null;
    }

    private void initData() {

        getBranchBankList();

    }


    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String etValues = s.toString();
            updateAdapter(etValues);
        }

    };


    /**
     * 根据撒选条件，选择数据
     * @param keyValue
     */
    private void updateAdapter(String keyValue) {

        List<String> listTemp = new ArrayList<String>();
        if (!TextUtils.isEmpty(keyValue)) {
            for (String temp : listDatas) {
                if (temp.contains(keyValue)) {
                    listTemp.add(temp);
                }
            }
        } else {
            listTemp = listDatas;
        }
        adapter = new AdapterItem1Heigth40TextSize15(mContext,listTemp);
       lv_settlebankname_bankname.setAdapter(adapter);
    }


    public void getBranchBankList() {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("bankType",getIntent().getStringExtra("bankType"));
        map.put("city",getIntent().getStringExtra("city"));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.CardCnapsSearch, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    //data
                    branchBankEntities = new Gson().fromJson(jsonObject.getJSONArray("cnapsList").toString(), new TypeToken<List<BranchBankEntity>>() {}.getType());
                    if (branchBankEntities != null && !branchBankEntities.isEmpty()) {
                        for (BranchBankEntity branchBankEntity:branchBankEntities) {
                            listDatas.add(branchBankEntity.getBankName());
                        }
                    }
                    //adapter
                    adapter = new AdapterItem1Heigth40TextSize15(mContext,listDatas);
                    // bind
                    lv_settlebankname_bankname.setAdapter(adapter);
                    et_settlebankname_bankname.addTextChangedListener(textWatcher);
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }
}
